package com.gs.synedocket.model;


import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.callback.Callback;
import com.gs.synedocket.callback.CallbackWithProgress;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.DbMsg;
import com.gs.synedocket.db.dto.Base64PhotoDto;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.model.listener.OnResultProgressListener;
import com.gs.synedocket.util.AppUtil;
import com.gs.synedocket.util.OKHttpUtil;
import com.gs.synedocket.util.RSAUtil;

import java.io.File;
import java.lang.reflect.Type;
import java.util.Map;

public class UtilModel {
    public void uploadOffline(String path, final OnResultListener listener) {
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPLOAD_SUCCESS);
        result.setData(path);
        listener.onSuccess(result);
    }

    public void upload(String path, final OnResultListener listener, Type typeofT) {
        OKHttpUtil.getInstance().uploadFileSync(OKHttpUtil.NET_URL + "/api/utils/uploadfile", path, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                listener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                listener.onFails(e);
            }
        }, typeofT);
    }

    public void uploadBase64Offline(Base64PhotoDto bean, final OnResultListener listener) {
        String encrypt = bean.getBase64();
        if (bean.isEncrypted() != null && bean.isEncrypted()) {
            Map<Integer, String> key = RSAUtil.getKey();
            if (!key.isEmpty()) {
                encrypt = RSAUtil.encipher(encrypt, key.get(0));
                encrypt = encrypt + AllConstants.SPLIT + key.get(1);
            }
        }
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPLOAD_SUCCESS);
        result.setData(AllConstants.PHOTO_NAME + encrypt);
        listener.onSuccess(result);
    }

    public void uploadBase64(Base64PhotoDto bean, final OnResultListener listener,Type typeofT) {
        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/utils/uploadfilebase64", bean, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                listener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                listener.onFails(e);
            }
        }, typeofT);
    }

    public void download(String fileName, File file, final OnResultProgressListener listener) {
        OKHttpUtil.getInstance().downloadFile(OKHttpUtil.NET_URL + "/api/WebServices/download/" + fileName, file,
                new CallbackWithProgress() {
                    @Override
                    public void onSuccess(Object obj) {
                        listener.onSuccess(obj);
                    }

                    @Override
                    public void onFailed(Exception e) {
                        listener.onFails(e);
                    }

                    @Override
                    public void onProgress(int progress) {
                        listener.onProgress(progress);
                    }
                });
    }

    public void downloadMulti(String fileName, File file, final OnResultProgressListener listener) {
        OKHttpUtil.getInstance().downloadFileMulti(OKHttpUtil.NET_URL + "/api/WebServices/download/" + fileName, file,
                new CallbackWithProgress() {
                    @Override
                    public void onSuccess(Object obj) {
                        listener.onSuccess(obj);
                    }

                    @Override
                    public void onFailed(Exception e) {
                        listener.onFails(e);
                    }

                    @Override
                    public void onProgress(int progress) {
                        listener.onProgress(progress);
                    }
                });
    }

    public void getVersion(final OnResultListener listener, Type typeofT) {
        OKHttpUtil.getInstance().getVersion(OKHttpUtil.NET_URL + "/api/WebServices/appInfo?applicationId=" + AppUtil.getInstance().getContext().getPackageName(),
                new Callback() {
                    @Override
                    public void onSuccess(Object obj) {
                        listener.onSuccess(obj);
                    }

                    @Override
                    public void onFailed(Exception e) {
                        listener.onFails(e);
                    }
                }, typeofT);
    }
}
